package com.nike.amoeba.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.TimeUtils
import com.google.gson.Gson
import com.nike.amoeba.R
import com.nike.amoeba.base.MyBaseViewModel
import com.nike.amoeba.bean.AppRoomDataBase
import com.nike.amoeba.bean.RfidStateBean
import com.nike.amoeba.bean.db.AssetBean
import com.nike.amoeba.bean.db.UploadOrderBean
import com.nike.amoeba.bean.db.UploadOrderListBean
import com.nike.amoeba.ext.INVENTORY_FAIL
import com.nike.amoeba.ext.UPLOAD_ASSET
import com.nike.amoeba.ext.UPLOAD_IMAGE_SPLIT
import com.nike.amoeba.ext.showMessage
import com.nike.amoeba.ui.frg.AssetFrg
import com.nike.amoeba.util.CacheUtil
import com.nike.amoeba.util.ImageUtils
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Flowable
import io.reactivex.rxjava3.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField
import org.json.JSONArray
import org.json.JSONObject

/**
 * @Author nike
 * @Date 2023/7/27 11:45
 * @Description
 */
open class StockTextModel : MyBaseViewModel() {

    var listJsonArray: MutableLiveData<JSONArray> = MutableLiveData()

    fun onRequestText(bean: String?) {
        val jsonArray = JSONArray(bean)
        Flowable.fromCallable {
            val list = JSONArray()
            val obj = JSONObject()
            obj.put("title", appContext.getString(R.string.detailed))
            val ja = JSONArray()
            for (i in 0 until jsonArray.length()) {
                val obj = jsonArray.optJSONObject(i)
                var objBean = JSONObject()
                objBean.put("title", obj.optString("Title"))
                objBean.put("content", obj.optString("content"))
                ja.put(objBean)
            }
            obj.put("list", ja)
            list.put(obj)
            list
        }.subscribeOn(Schedulers.io()) // 给上面分配了异步线程
            .observeOn(AndroidSchedulers.mainThread()) // 给下面分配了UI线程
            .subscribe({ list ->
                listJsonArray.value = list
            }, { error ->
                // 处理错误
                LogUtils.e(error)
            })
    }

}